package com.ms.module.wechat.clear.fragment.clear;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.navigation.NavController;
import androidx.navigation.Navigation;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.entity.node.BaseNode;
import com.chad.library.adapter.base.provider.BaseNodeProvider;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.ms.module.wechat.clear.R;

import com.ms.module.wechat.clear.fragment.adapter.node.DownChildNode;
import com.ms.module.wechat.clear.utils.ByteSizeToStringUnitUtils;


import org.jetbrains.annotations.NotNull;

public class DownChildProvider extends BaseNodeProvider {

    @Override
    public int getItemViewType() {
        return 4;
    }

    @Override
    public int getLayoutId() {
        return R.layout.provider_wechat_clear_down_child;
    }

    @Override
    public void convert(@NotNull BaseViewHolder baseViewHolder, BaseNode baseNode) {

        ImageView imageViewIcon = baseViewHolder.findView(R.id.imageViewIcon);
        TextView textViewName = baseViewHolder.findView(R.id.textViewName);
        TextView textViewSize = baseViewHolder.findView(R.id.textViewSize);
        ImageView imageViewEnter = baseViewHolder.findView(R.id.imageViewEnter);


        if (baseNode instanceof DownChildNode) {
            DownChildNode downChildNode = (DownChildNode) baseNode;
            Glide.with(getContext()).load(downChildNode.getIcon()).into(imageViewIcon);
            textViewName.setText(downChildNode.getName());
            long length = downChildNode.getSize();
            if (length == 0) {
                textViewSize.setText("未发现");
                imageViewEnter.setVisibility(View.INVISIBLE);

            } else {
                textViewSize.setText(ByteSizeToStringUnitUtils.byteToStringUnit(length));
                imageViewEnter.setVisibility(View.VISIBLE);
            }


            baseViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if ("聊天图片".equals(textViewName.getText().toString())) {
                        if (length != 0) {
                            NavController navController = Navigation.findNavController((Activity) context, R.id.fragment);
                            navController.setGraph(R.navigation.com_ms_module_wechat_clear_navigation);
                            Bundle bundle = new Bundle();
                            bundle.putString("type", "image");
                            navController.navigate(R.id.detailsFragment, bundle);
                        }
                    } else if ("聊天表情包".equals(textViewName.getText().toString())) {
                        if (length != 0) {
                            NavController navController = Navigation.findNavController((Activity) context, R.id.fragment);
                            navController.setGraph(R.navigation.com_ms_module_wechat_clear_navigation);
                            Bundle bundle = new Bundle();
                            bundle.putString("type", "emoji");
                            navController.navigate(R.id.detailsFragment, bundle);
                        }

                    } else if ("文件".equals(textViewName.getText().toString())) {
                        if (length != 0) {
                            NavController navController = Navigation.findNavController((Activity) context, R.id.fragment);
                            navController.setGraph(R.navigation.com_ms_module_wechat_clear_navigation);
                            Bundle bundle = new Bundle();
                            bundle.putString("type", "file");
                            navController.navigate(R.id.detailsFragment, bundle);
                        }

                    } else if ("聊天语音".equals(textViewName.getText().toString())) {
                        if (length != 0) {
                            NavController navController = Navigation.findNavController((Activity) context, R.id.fragment);
                            navController.setGraph(R.navigation.com_ms_module_wechat_clear_navigation);
                            Bundle bundle = new Bundle();
                            bundle.putString("type", "voice");
                            navController.navigate(R.id.detailsFragment, bundle);
                        }

                    } else if ("聊天小视频".equals(textViewName.getText().toString())) {
                        if (length != 0) {
                            NavController navController = Navigation.findNavController((Activity) context, R.id.fragment);
                            navController.setGraph(R.navigation.com_ms_module_wechat_clear_navigation);
                            Bundle bundle = new Bundle();
                            bundle.putString("type", "video");
                            navController.navigate(R.id.detailsFragment, bundle);
                        }
                    }
                }
            });


            imageViewEnter.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if ("聊天图片".equals(textViewName.getText().toString())) {
                        if (length != 0) {
                            NavController navController = Navigation.findNavController((Activity) context, R.id.fragment);
                            navController.setGraph(R.navigation.com_ms_module_wechat_clear_navigation);
                            Bundle bundle = new Bundle();
                            bundle.putString("type", "image");
                            navController.navigate(R.id.detailsFragment, bundle);
                        }
                    } else if ("聊天表情包".equals(textViewName.getText().toString())) {
                        if (length != 0) {
                            NavController navController = Navigation.findNavController((Activity) context, R.id.fragment);
                            navController.setGraph(R.navigation.com_ms_module_wechat_clear_navigation);
                            Bundle bundle = new Bundle();
                            bundle.putString("type", "emoji");
                            navController.navigate(R.id.detailsFragment, bundle);
                        }

                    } else if ("文件".equals(textViewName.getText().toString())) {
                        if (length != 0) {
                            NavController navController = Navigation.findNavController((Activity) context, R.id.fragment);
                            navController.setGraph(R.navigation.com_ms_module_wechat_clear_navigation);
                            Bundle bundle = new Bundle();
                            bundle.putString("type", "file");
                            navController.navigate(R.id.detailsFragment, bundle);
                        }

                    } else if ("聊天语音".equals(textViewName.getText().toString())) {
                        if (length != 0) {
                            NavController navController = Navigation.findNavController((Activity) context, R.id.fragment);
                            navController.setGraph(R.navigation.com_ms_module_wechat_clear_navigation);
                            Bundle bundle = new Bundle();
                            bundle.putString("type", "voice");
                            navController.navigate(R.id.detailsFragment, bundle);
                        }

                    } else if ("聊天小视频".equals(textViewName.getText().toString())) {
                        if (length != 0) {
                            NavController navController = Navigation.findNavController((Activity) context, R.id.fragment);
                            navController.setGraph(R.navigation.com_ms_module_wechat_clear_navigation);
                            Bundle bundle = new Bundle();
                            bundle.putString("type", "video");
                            navController.navigate(R.id.detailsFragment, bundle);
                        }
                    }
                }
            });
        }
    }
}
